<?php

namespace App\Models;

use App\Models\Traits\HasDates;
use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\OrderRefund
 *
 * @property int $refund_id 主键
 * @property int $uid 用户ID
 * @property int $order_id 订单ID
 * @property string|null $refund_no 退款单号
 * @property int $refund_type 退货类型,1=仅退款,2=退货退款
 * @property int $refund_state 处理状态
 * @property string|null $refund_reason 退货原因
 * @property string|null $refund_desc 退款说明
 * @property string $refund_amount 退款金额
 * @property string|null $refund_remark 备注
 * @property string $shipping_fee 退货运费
 * @property int $receive_state 货物状态
 * @property \Illuminate\Support\Carbon|null $created_at 创建时间
 * @property \Illuminate\Support\Carbon|null $updated_at 更新时间
 * @property-read array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null $receive_state_des
 * @property-read array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null $refund_state_des
 * @property-read array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null $refund_type_des
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RefundImage[] $images
 * @property-read int|null $images_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderItem[] $items
 * @property-read int|null $items_count
 * @property-read \App\Models\Order $order
 * @property-read Refund $refund
 * @property-read \App\Models\RefundShipping|null $shipping
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|Refund newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Refund newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Refund query()
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereReceiveState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereRefundType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereShippingFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Refund whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Refund extends Model
{
    use HasDates;
    protected $table = 'refund';
    protected $primaryKey = 'refund_id';
    protected $fillable = [
        'uid', 'order_id', 'refund_no', 'refund_type', 'refund_state', 'refund_remark',
        'refund_reason', 'refund_desc', 'refund_amount', 'shipping_fee', 'receive_state'
    ];
    protected $appends = ['refund_type_des', 'refund_state_des', 'receive_state_des'];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::deleting(function (Refund $refund) {
            $refund->images()->delete();
            $refund->shipping()->delete();
            $refund->items()->update(['refund_state' => 0, 'refund_id' => 0]);
        });
    }

    /**
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
     */
    public function getRefundTypeDesAttribute()
    {
        return trans('trade.refund_types.' . $this->refund_type);
    }

    /**
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
     */
    public function getRefundStateDesAttribute()
    {
        return trans('trade.refund_states.' . $this->refund_state);
    }

    /**
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
     */
    public function getReceiveStateDesAttribute()
    {
        return trans('trade.receive_states.' . $this->receive_state);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function images()
    {
        return $this->hasMany(RefundImage::class, 'refund_id', 'refund_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne|RefundShipping
     */
    public function shipping()
    {
        return $this->hasOne(RefundShipping::class, 'refund_id', 'refund_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function items()
    {
        return $this->hasMany(OrderItem::class, 'refund_id', 'refund_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function refund()
    {
        return $this->belongsTo(Refund::class, 'refund_id', 'refund_id');
    }

    /**
     * @param $refund_no
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null|Refund
     */
    public static function findByRefundNo($refund_no)
    {
        return Refund::where('refund_no', $refund_no)->first();
    }
}
